//
//  ViewController.swift
//  2-12-Swift闭包循环引用及其解决方法
//
//  Created by 范英强 on 16/1/26.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var closure: (()->())?
    override func viewDidLoad() {
        super.viewDidLoad()
        //ViewController 强引用了 cl0sure
        //closure 强引用了 self.view (ViewController)
        
        //解决强引用的方法
        //1.仿OC
//        weak var weakSelf = self
        //2.在闭包内使用[weak self] 如果对象销毁了 指向的是nil
//        closure = {[unowned self] in
//            print(self?.view)
//        }
        //3.闭包内使用[unownerd self]  如果 对象销毁了 self指向的地址还是原地址 --->野指针
        closure = {[unowned self] in
            print(self.view)
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        closure?()
    }
    //析构函数
    deinit{
        //1.通知销毁
        //2.kvc
        //3.NSTimer
        print("再见")
        
    }
}

